package com.yanqu.road.dao.impl.activity.ghost;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ghost.GhostDispersePlayer;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GhostDispersePlayerDaoImpl extends TempDataObjectDao<GhostDispersePlayer> {

    public static String getGhostDispersePlayerMapSql = "select * from t_c_activity_ghost_disperse_player where activityId=?";

    public Map<Integer, Map<Integer, GhostDispersePlayer>> getGhostDispersePlayerMap(int activityId) {
        Map<Integer, Map<Integer, GhostDispersePlayer>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGhostDispersePlayerMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GhostDispersePlayer ghostDispersePlayer = parse(rs);
                if (!map.containsKey(ghostDispersePlayer.getBatchNo())) {
                    map.put(ghostDispersePlayer.getBatchNo(), new ConcurrentHashMap<>());
                }
                map.get(ghostDispersePlayer.getBatchNo()).put(ghostDispersePlayer.getType(), ghostDispersePlayer);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateGhostDispersePlayerSql = "update t_c_activity_ghost_disperse_player set batchNo=?,type=?,userId=?,userBaseInfo=?,time=?";

    @Override
    protected boolean updateData(GhostDispersePlayer ghostDispersePlayer) {
        ghostDispersePlayer.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostDispersePlayer.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDispersePlayer.getBatchNo()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDispersePlayer.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostDispersePlayer.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(ghostDispersePlayer.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, ghostDispersePlayer.getTime()));
        if (getDb().executeNoneQuery(updateGhostDispersePlayerSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGhostDispersePlayerSql = "insert into t_c_activity_ghost_disperse_player(activityId,batchNo,type,userId,userBaseInfo,time) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(GhostDispersePlayer ghostDispersePlayer) {
        ghostDispersePlayer.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostDispersePlayer.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDispersePlayer.getBatchNo()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDispersePlayer.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostDispersePlayer.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(ghostDispersePlayer.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, ghostDispersePlayer.getTime()));
        if (getDb().executeNoneQuery(insertGhostDispersePlayerSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GhostDispersePlayer parseData(ResultSet rs) throws SQLException {
        GhostDispersePlayer ghostDispersePlayer = new GhostDispersePlayer();
        ghostDispersePlayer.setActivityId(rs.getInt("activityId"));
        ghostDispersePlayer.setBatchNo(rs.getInt("batchNo"));
        ghostDispersePlayer.setType(rs.getInt("type"));
        ghostDispersePlayer.setUserId(rs.getLong("userId"));
        String userBaseInfo = rs.getString("userBaseInfo");
        ghostDispersePlayer.setUserBaseInfo(StringUtils.isNullOrEmpty(userBaseInfo) ? new UserBaseInfo() : JSONObject.parseObject(userBaseInfo, new TypeReference<UserBaseInfo>() {
        }));
        ghostDispersePlayer.setTime(rs.getLong("time"));

        return ghostDispersePlayer;
    }

}
